//

//  health
//
//  Created by apple on 2021/11/9.
//  Copyright © 2021 zglee. All rights reserved.
//

#import "MJBloodSugarModel.h"
#import <WCDBObjc/WCDBObjc.h>
@implementation MJBloodSugarModel

WCDB_IMPLEMENTATION(MJBloodSugarModel) //使用WCDB_IMPLEMENTATION宏在类文件定义绑定到数据库表的类。 该宏实现绑定到表

WCDB_SYNTHESIZE(detectionId)
WCDB_SYNTHESIZE(userId)
WCDB_SYNTHESIZE(userName)
WCDB_SYNTHESIZE(patientId)
WCDB_SYNTHESIZE(patientName)
WCDB_SYNTHESIZE(age)
WCDB_SYNTHESIZE(gender)
WCDB_SYNTHESIZE(detectionDateTime)
WCDB_SYNTHESIZE(createDateTime)
WCDB_SYNTHESIZE(period)
WCDB_SYNTHESIZE(orgName)
WCDB_SYNTHESIZE(isMultiple)

WCDB_SYNTHESIZE(bloodGlucose)
WCDB_SYNTHESIZE(prediction)
WCDB_SYNTHESIZE(evaluation)
WCDB_SYNTHESIZE(unit)

WCDB_PRIMARY(detectionId)

- (void)switchUnit {
    if([self.unit isEqualToString:unitmgdL]){
        self.unit = unitmmolL;
        self.bloodGlucose = self.bloodGlucose/18;
    }else{
        self.unit = unitmgdL;
        self.bloodGlucose = self.bloodGlucose*18;
    }
}

- (BOOL)showArrowsUp {
//    return self.gradeValue > 1 ? true:false;
    
    if([self.unit isEqualToString:unitmgdL]){
        if(self.isFasting){
            if(_bloodGlucose >= 110){
                return true;
            }
        }else{
            if(_bloodGlucose >= 140){
                return true;
            }
        }
    }else{
        if(self.isFasting){
            if(_bloodGlucose >= 6.1){
                return true;
            }
        }else{
            if(_bloodGlucose >= 7.8){
                return true;
            }
        }
    }
    return false;
}

- (BOOL)showArrowsDown {
//    return self.gradeValue == 0 ? true:false;
    
    if([self.unit isEqualToString:unitmgdL]){
        if(self.isFasting){
            if(_bloodGlucose < 70){
                return true;
            }
        }else{
            if(_bloodGlucose < 70){
                return true;
            }
        }
    }else{
        if(self.isFasting){
            if(_bloodGlucose < 3.9){
                return true;
            }
        }else{
            if(_bloodGlucose < 3.9){
                return true;
            }
        }
    }
    return false;
}

- (NSString *)getBloodGlucoseString:(BOOL)showHi {
    
    if([self.unit isEqualToString:unitmgdL]){
        NSString *value;
        if (_bloodGlucose == 0) {
            value = @"--";
        }else if (_bloodGlucose<20){
            value = showHi ? @"Lo" : @"<20";
        }else if (_bloodGlucose>600){
            value = showHi ? @"Hi" : @">600";
        }else{
            value = [NSString mj_stringWithCGFloat:_bloodGlucose decimal:0];
        }
        return value;
    }else{
        NSString *value;
        if (_bloodGlucose == 0) {
            value = @"--";
        }else if (_bloodGlucose<1.1){
            value = showHi ? @"Lo" : @"<1.1";
        }else if (_bloodGlucose>33.3){
            value = showHi ? @"Hi" : @">33.3";
        }else{
            value = [NSString mj_stringWithCGFloat:_bloodGlucose decimal:2];
        }
        return value;
    }
}

- (NSString *)bloodGlucoseString {
    return [self getBloodGlucoseString:true];
}

- (NSString *)bloodGlucoseChartString {
    return [self getBloodGlucoseString:false];
}

- (NSInteger)gradeValue {
    if([self.prediction isEqualToString:@"Hypoglycemia"]){
        return 0;
    }else if([self.prediction isEqualToString:@"Normal"]){
        return 1;
    }else if([self.prediction isEqualToString:@"Prediabetes"]){
        return 2;
    }else if([self.prediction isEqualToString:@"Diabetes"]){
        return 3;
    }else if([self.prediction isEqualToString:@"Ketoacidosis"]){
        return 4;
    }else{
        return 0;
    }
}

- (NSString *)evaluationString {
    if(self.isFasting){
        return [@"BloodGlucose.evaluation.Fasting." stringByAppendingString:_evaluation];
    }else{
        return [@"BloodGlucose.evaluation.Postprandial." stringByAppendingString:_evaluation];
    }
    
}

- (NSString *)differenceValue {
    NSString *value;
    if([self.unit isEqualToString:unitmgdL]){
        if(self.isFasting){
            if (self.bloodGlucose > 109) {
                value = [NSString mj_stringWithCGFloat:self.bloodGlucose - 109 decimal:0];
            }
            if (self.bloodGlucose < 70) {
                value = [NSString mj_stringWithCGFloat:70 - self.bloodGlucose decimal:0];
            }
        }else{
            if (self.bloodGlucose > 139) {
                value = [NSString mj_stringWithCGFloat:self.bloodGlucose - 139 decimal:0];
            }
            if (self.bloodGlucose < 70) {
                value = [NSString mj_stringWithCGFloat:70 - self.bloodGlucose decimal:0];
            }
        }
    }else{
        if(self.isFasting){
            if (self.bloodGlucose > 6.09) {
                value = [NSString mj_stringWithCGFloat:self.bloodGlucose - 6.09 decimal:2];
            }
            if (self.bloodGlucose < 3.9) {
                value = [NSString mj_stringWithCGFloat:3.9 - self.bloodGlucose decimal:2];
            }
        }else{
            if (self.bloodGlucose > 7.79) {
                value = [NSString mj_stringWithCGFloat:self.bloodGlucose - 7.79 decimal:2];
            }
            if (self.bloodGlucose < 3.9) {
                value = [NSString mj_stringWithCGFloat:3.9 - self.bloodGlucose decimal:2];
            }
        }
    }
    NSString *xxx = @"";
    if(self.showArrowsUp){
        xxx = @"-";
    }
    if(self.showArrowsDown){
        xxx = @"+";
    }
    return [NSString stringWithFormat:@"%@%@",xxx,value];
}
- (NSString *)standardValue {
    
    if([self.unit isEqualToString:unitmgdL]){
        if(self.isFasting){
            if (self.bloodGlucose > 109) {
                return @(109).description;
            }
            if (self.bloodGlucose < 70) {
                return @(70).description;
            }
        }else{
            if (self.bloodGlucose > 139) {
                return @(139).description;
            }
            if (self.bloodGlucose < 70) {
                return @(70).description;
            }
        }
    }else{
        if(self.isFasting){
            if (self.bloodGlucose > 6.09) {
                return @(6.09).description;
            }
            if (self.bloodGlucose < 3.9) {
                return @(3.9).description;
            }
        }else{
            if (self.bloodGlucose > 7.79) {
                return @(7.79).description;
            }
            if (self.bloodGlucose < 3.9) {
                return @(3.9).description;
            }
        }
    }
    return @"";

}

- (NSString *)printStandardValue {
    
    if([self.unit isEqualToString:unitmgdL]){
        if(self.isFasting){
            return @"70 ~ 109";
        }else{
            return @"70 ~ 139";
        }
    }else{
        if(self.isFasting){
            return @"3.9 ~ 6.09";
        }else{
            return @"3.9 ~ 7.79";
        }
    }
    return @"";
}

@end

